<?php

namespace Api\Controller;

class MemberController extends ApiController {

    private $token;
    private $deviceid;
    private $user_id;
    private $class;
    private $error_member;
    private $userinfo;

    function __construct() {
        parent::__construct();
        $this->token = I('request.token');
        $this->deviceid = I('request.deviceid');
        $this->userinfo = $this->verify_token($this->token, $this->deviceid);
        $this->user_id = $this->userinfo['user_id'];
        $this->error_member = L('error_member');
    }

    public function joinclassbycode() {
        $warntype = 1;
        $code = I('request.code');
        if (!preg_match('/^[\w]{8}$/', $code)) {
            $this->error($this->error_member['longererror']);
        }
        if (I('request.name') != FALSE) {
            if (!preg_match('/^[\x{4e00}-\x{9fa5}]{2,6}$/u', I('request.name'))) {
                $this->error($this->error_reg['nonick']);
            }
            $rename = I('request.name');
        } else {
            $rename = $user['nickname'];
        }
        //查找班级
        $where = array('code' => $code);
        $class = M('Class')->where($where)->find();
        if (empty($class['id'])) {
            $this->error($this->error_member['nocode']);
        }
        //是否已在班级
        $where = array('user_id' => $this->user_id, 'class_id' => $class['id']);
        if (M('Classmember')->where($where)->find()) {
            $this->error($this->error_member['inclass']);
        }
        //加入班级上线
        $where = array('user_id' => $this->user_id);
        $num = M('Classmember')->where($where)->count('id');
        if ($num >= $this->classnum) {
            $this->error($this->error_member['classnum']);
        }
        $Member = D('Member');
        $User = D('Users');
        $user = $User->findUserbyId($this->user_id);
        $remarks = I('request.remarks');
        $Member->addMember($user['id'], $class['id'], $rename, $remarks);
        //警告记录
        $warn_content = array(
            'username' => $user['username'],
            'classname=' => $class['classname'],
        );
        $this->warnclass($class['user_id'], $rename, $class['classname']);
        $arrInfo = array(
            'classid' => $class['id'],
            'classname' => $class['classname']
        );
        $this->success(__FUNCTION__, $arrInfo);
    }

    public function changemember() {
        $iCid = I('request.classid');
        $aData['rename'] = I('request.rename');
        $aData['remarks'] = I('request.remarks');
        $iUid=  $this->user_id;
        //验证空值
        if (empty($iCid)) {
            $this->error($this->error_member['oserror']);
        }
        if (empty($aData['rename']) && empty($aData['remarks'])) {
            $this->error($this->error_member['nullupdata']);
        }
        //班级存在
        $mCmember = D('Member');
        $aCmember = $mCmember->getMembyids($iUid, $iCid);
        if (empty($aCmember['id'])) {
            $this->error($this->error_member['noinclass']);
        }
        //更新数据
        foreach ($aData as $key => $value) {
            if ($aCmember[$key] != $value) {
                $aUpdata[$key] = $value;
            }
        }
        //修改
        if (empty($aUpdata)) {
            $this->error($this->error_member['sameupdata']);
        } else {
            $mCmember->upMemberbyids($iUid, $iCid, $aUpdata);
        }
        $this->success(__FUNCTION__, $aData);
    }

    public function viewmember() {
        $iUid = I('request.userid');
        $iCid = I('request.classid');
        $mCmember = D('Member');
        $aCmember = $mCmember->getMembyids($this->user_id, $iCid);
        if (empty($aCmember['power'])) {
            $this->error($this->error_member['nopower']);
        }
        $aCmember = $mCmember->getCmemberCbyids($iUid, $iCid);

        if (empty($aCmember['class_id'])) {
            $this->error($this->error_member['noinclass']);
        }
        $this->success(__FUNCTION__, $aCmember);
    }

    public function quit() {
        $iCid = I('request.classid');
        if (empty($iCid)) {
            $this->error($this->error_member['invalid']);
        }
        $mCmember = D('Member');
        if (!$mCmember->getMembyids($this->user_id, $iCid)) {
            $this->error($this->error_member['noinclass']);
        }
        if ($mCmember->delMemberbyids($this->user_id, $iCid)) {
            $info = $this->error_member['quitok'];
            $this->success(__FUNCTION__, $info);
        } else {
            $this->error($this->error_member['oserror']);
        }
    }

    protected function warnclass($iUid, $sUsername, $sCname) {
        $sContent = $sUsername . '申请加入' . $sCname;
        $this->jpushapi(array($iUid), $sContent, '', 2);
    }

}
